#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/30  9:35
# @Author: 余浪人
# @email: yulangren520@gmail.com
import random
from datetime import datetime
from apps.lib.cache_sys import sys_cache, cache
from apps.lib.public import get_date, redis_, visiting_user, check_obj
from apps.models.system_model import TL_Blogroll_Model, TL_Notice_Model, TL_Ad_Model
from apps.webapi import webApi_bp
from flask import jsonify, request, make_response
from apps.models.content_model import TL_Content_Model, TL_Column_model, TL_Comment_Model, db
from apps.webapi.webpublic.public import page_auto, content, apiErrors, notNull, img, redis_cli,click_up, authority, conmmentAuthority, ipAdd, ipAuthority, nav_list
@webApi_bp .route ('/IndexList',endpoint ='IndexList',methods =['GET'])
@ipAuthority
@authority
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='IndexList')
def indexList ():
    O00O0OO0000OOO00O =TL_Content_Model .query .order_by (TL_Content_Model .is_index_stick .desc ()).order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status ==1 ,TL_Content_Model .update_time <datetime .now ())
    O00O00000OO000000 ,O0OO0O00O00OOOO0O =page_auto (O00O0OO0000OOO00O ,20 ,4 )
    O00O00000OO000000 ={"status":0 ,'msg':'successful','presentPage':O0OO0O00O00OOOO0O .page ,"dataList":content (O00O00000OO000000 ,'%Y-%m-%d')}
    return jsonify (O00O00000OO000000 )
@webApi_bp .route ('/nav',endpoint ='nav',methods =['GET'])
@ipAuthority
@ipAdd
@authority
@visiting_user
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='api_nav')
def nav ():
    OOOO000O0O0O00OO0 =TL_Column_model .query .order_by (TL_Column_model .column_order .desc ()).order_by (TL_Column_model .id .asc ()).filter (TL_Column_model .status ==1 ).all ()
    OO00OOOO00OO0OOOO ={"status":0 ,'msg':'successful','amount':len (OOOO000O0O0O00OO0 ),'dataList':nav_list (OOOO000O0O0O00OO0 )}
    return jsonify (OO00OOOO00OO0OOOO )
@webApi_bp .route ('/randomRecommend',endpoint ='randomRecommend',methods =['GET'])
@ipAuthority
@authority
def random_recommend ():
    O0O000OO0O0O0O0OO =TL_Content_Model .query .filter (TL_Content_Model .status ==1 ,TL_Content_Model .update_time <datetime .now ()).count ()
    OO0O00O0OOO00OO0O =random .sample (TL_Content_Model .query .filter (TL_Content_Model .status ==1 ,TL_Content_Model .update_time <datetime .now ()).all (),20 if O0O000OO0O0O0O0OO >=20 else O0O000OO0O0O0O0OO )
    OOOO0O00O0OO0O00O ={"status":0 ,'msg':'successful','dataList':content (OO0O00O0OOO00OO0O ,'%Y-%m-%d')}
    return jsonify (OOOO0O00O0OO0O00O )
@webApi_bp .route ('/blogroll',endpoint ='blogroll',methods =['GET'])
@ipAuthority
@authority
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='api_blogroll')
def blogroll ():
    OO000O00O0OO0OO0O =TL_Blogroll_Model .query .order_by (TL_Blogroll_Model .link_sort .desc ()).filter (TL_Blogroll_Model .status ==1 ,TL_Blogroll_Model .link_end_date >=datetime .now ()).all ()
    O00OOO00O00O0OO0O ={"status":0 ,'msg':'successful','dataList':[{"link_name":O0O0OO000OOOOOO00 .link_name ,"link_url":O0O0OO000OOOOOO00 .link_url ,"link_color":O0O0OO000OOOOOO00 .link_color ,}for O0O0OO000OOOOOO00 in OO000O00O0OO0OO0O ]}
    return jsonify (O00OOO00O00O0OO0O )
@webApi_bp .route ('/clickOrder',endpoint ='clickOrder',methods =['GET'])
@ipAuthority
@authority
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='api_clickOrder')
def click_order ():
    O00000O0OO0O0O0O0 =TL_Content_Model .query .order_by (TL_Content_Model .clicks .desc ()).filter (TL_Content_Model .update_time <datetime .now ()).all ()[0 :50 ]
    O0OO0OOOO0O000OOO ={"status":0 ,'msg':'successful','dataList':content (O00000O0OO0O0O0O0 ,'%Y-%m-%d')}
    return jsonify (O0OO0OOOO0O000OOO )
@webApi_bp .route ('/columnUpdate',endpoint ='columnUpdate',methods =['POST'])
@ipAuthority
@authority
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='api_columnUpdate')
def columnUpdate ():
    OO00O0OO0OOO0OOOO =TL_Column_model .query .order_by (TL_Column_model .column_order .asc ()).filter ().all ()
    OO0OOOOO0O0O0O000 ={"status":0 ,'msg':'successful','colList':[{'column_name':O0000OO000OO00O00 .column_name ,'column_link':O0000OO000OO00O00 .column_link ,'column_ID':O0000OO000OO00O00 .uuid ,'dataList':content (O0000OO000OO00O00 .content_model [:50 ],'%Y-%m-%d')}for O0000OO000OO00O00 in OO00O0OO0OOO0OOOO ]}
    return jsonify (OO0OOOOO0O0O0O000 )
@webApi_bp .route ('/columnList',endpoint ='columnList',methods =['POST'])
@ipAuthority
@authority
@visiting_user
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='api_columnList')
def columnList ():
    OO0OO0000OOOO00OO =request .json .get ('PID')if request .is_json else request .form .get ('PID')
    OO0O000000OOOO000 =TL_Column_model .query .filter_by (uuid =OO0OO0000OOOO00OO ).first ()
    if apiErrors (OO0O000000OOOO000 ):return apiErrors (OO0O000000OOOO000 )
    OOOO0O0OO00O00O00 =TL_Content_Model .query .order_by (TL_Content_Model .is_list_stick .desc ()).order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status ==1 ,TL_Content_Model .superior ==OO0O000000OOOO000 .id )
    OOOOO0OO00OO0O00O ,O0OOOO0O000OOO0OO =page_auto (OOOO0O0OO00O00O00 ,10 ,)
    O0O00O0OOOOOOO00O ={"status":0 ,'msg':'successful','PID':OO0O000000OOOO000 .uuid ,'pageList':{"total":O0OOOO0O000OOO0OO .total ,"prev":O0OOOO0O000OOO0OO .has_prev ,"prevPage":notNull (O0OOOO0O000OOO0OO .prev_num ),"next":O0OOOO0O000OOO0OO .has_next ,'nextPage':notNull (O0OOOO0O000OOO0OO .next_num ),'currentPage':O0OOOO0O000OOO0OO .page ,'pages':O0OOOO0O000OOO0OO .pages ,'iter_pages':[O00O00O0O00OOO000 for O00O00O0O00OOO000 in O0OOOO0O000OOO0OO .iter_pages (2 ,2 ,2 ,2 )if O00O00O0O00OOO000 ]},'dataList':content (OOOOO0OO00OO0O00O ,'%Y-%m-%d')}
    return jsonify (O0O00O0OOOOOOO00O )
@webApi_bp .route ('/detail',endpoint ='detail',methods =['POST'])
@ipAuthority
@authority
@visiting_user
@click_up
@cache .cached (timeout =30 *(24 *(60 *60 )),key_prefix ='api_detail')
def detail ():
    OOO000OOO00OOO000 =request .json .get ('pid')if request .is_json else request .form .get ('pid')
    O00OOOO0000OO0O00 =TL_Content_Model .query .filter_by (uuid =OOO000OOO00OOO000 ,status =1 ).first ()
    if apiErrors (O00OOOO0000OO0O00 ):return apiErrors (O00OOOO0000OO0O00 )
    O0OO00O00O0OOO0O0 =O00OOOO0000OO0O00 .pro .content_model
    OOOOO0OO0O00OO0OO =2 if len (O0OO00O00O0OOO0O0 )>=2 else len (O0OO00O00O0OOO0O0 )
    O0000000OO0OOO000 =random .sample (O0OO00O00O0OOO0O0 ,OOOOO0OO0O00OO0OO )
    if len (O0000000OO0OOO000 )==2 :
        OO000OOOO0OOO0OO0 ={"title":O0000000OO0OOO000 [0 ].title ,"ID":O0000000OO0OOO000 [0 ].uuid ,'imgUrl':img (O0000000OO0OOO000 [0 ].thumb_img ,O0000000OO0OOO000 [0 ].content ),}
        O0O0O0OOO0000O00O ={"title":O0000000OO0OOO000 [1 ].title ,"ID":O0000000OO0OOO000 [1 ].uuid ,'imgUrl':img (O0000000OO0OOO000 [1 ].thumb_img ,O0000000OO0OOO000 [1 ].content ),}
    elif len (O0000000OO0OOO000 )==1 :
        OO000OOOO0OOO0OO0 ={"title":O0000000OO0OOO000 [0 ].title ,"ID":O0000000OO0OOO000 [0 ].uuid ,'imgUrl':img (O0000000OO0OOO000 [0 ].thumb_img ,O0000000OO0OOO000 [0 ].content ),}
        O0O0O0OOO0000O00O ={"title":'',"ID":'','imgUrl':'',}
    else :
        OO000OOOO0OOO0OO0 ={"title":'',"ID":'','imgUrl':''}
        O0O0O0OOO0000O00O ={"title":'',"ID":'','imgUrl':''}
    O0O00000O0OO0000O ={"status":0 ,"msg":'successful',"similar":content (O00OOOO0000OO0O00 .pro .content_model [:20 ][::-1 ],'%Y-%m-%d'),"data":{"title":O00OOOO0000OO0O00 .title ,'ID':O00OOOO0000OO0O00 .uuid ,'update_time':O00OOOO0000OO0O00 .update_time .strftime ('%Y-%m-%d'),'title_color':O00OOOO0000OO0O00 .title_color ,'imgUrl':img (O00OOOO0000OO0O00 .thumb_img ,O00OOOO0000OO0O00 .content ),'PID':O00OOOO0000OO0O00 .pro .column_name ,'author':O00OOOO0000OO0O00 .author ,'clicks':O00OOOO0000OO0O00 .clicks ,'contributor':O00OOOO0000OO0O00 .contributor ,'description':O00OOOO0000OO0O00 .description ,'content':O00OOOO0000OO0O00 .content ,'permissions':O00OOOO0000OO0O00 .permissions ,'download':O00OOOO0000OO0O00 .download ,'keyword':O00OOOO0000OO0O00 .keyword ,'is_focus':O00OOOO0000OO0O00 .is_focus ,'is_roll':O00OOOO0000OO0O00 .is_roll ,'is_roll_text':O00OOOO0000OO0O00 .is_roll_text ,'is_index_stick':O00OOOO0000OO0O00 .is_index_stick ,'is_list_stick':O00OOOO0000OO0O00 .is_list_stick ,'pay':O00OOOO0000OO0O00 .reward ,},"commentList":[{"comment_txt":O0O0OOO0000000000 .comment_txt ,"user_qq":O0O0OOO0000000000 .user_qq ,"user_name":O0O0OOO0000000000 .user_name ,"create_time":O0O0OOO0000000000 .create_time .strftime ('%Y-%m-%d'),}for O0O0OOO0000000000 in (O00OOOO0000OO0O00 .comment_model [:100 ])[::-1 ]if O0O0OOO0000000000 .status ],"next_page":OO000OOOO0OOO0OO0 ,"last_page":O0O0O0OOO0000O00O ,}
    return jsonify (O0O00000O0OO0000O )
@webApi_bp .route ('/notice',endpoint ='notice',methods =['GET'])
@ipAuthority
@authority
@cache .cached (timeout =48 *(60 *60 ),key_prefix ='api_notice')
def notice ():
    O0OO00000O000000O =TL_Notice_Model .query .order_by (TL_Notice_Model .id .desc ()).filter (TL_Notice_Model .status ==1 ).all ()
    O000O0O00O00O00O0 ={"status":0 ,"msg":'successful','data':[{'title':O0OOO00OO0O00O0OO .notice_title ,'promulgator':O0OOO00OO0O00O0OO .notice_promulgator ,'content':O0OOO00OO0O00O0OO .notice_content ,'date':O0OOO00OO0O00O0OO .update_time .strftime ('%m-%d'),'id':O0OOO00OO0O00O0OO .uuid ,}for O0OOO00OO0O00O0OO in O0OO00000O000000O ]}
    return jsonify (O000O0O00O00O00O0 )
@webApi_bp .route ('/lantern',endpoint ='lantern',methods =['GET'])
@ipAuthority
@authority
@cache .cached (timeout =24 *(60 *60 ),key_prefix ='api_lantern')
def lantern ():
    OO0OOOOOOOO0O0OOO =TL_Content_Model .query .order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status ==1 ,TL_Content_Model .is_roll ==1 ,TL_Content_Model .update_time <datetime .now ()).all ()
    OO00OOO00O00OOO0O ={"status":0 ,'msg':'successful',"dataList":content (OO0OOOOOOOO0O0OOO [:50 ])}
    redis_cli (request .path ,OO00OOO00O00OOO0O )
    return jsonify (OO00OOO00O00OOO0O )
@webApi_bp .route ('/webConf',methods =['GET'])
@ipAuthority
@authority
@cache .cached (timeout =48 *(60 *60 ),key_prefix ='api_webConf')
def web ():
    O0O00OO00O0OOOO00 =sys_cache ()
    OO0O0000000O0O00O ={"status":0 ,'msg':'successful',"data":{'web_name':O0O00OO00O0OOOO00 .web_name ,'web_subtitle':O0O00OO00O0OOOO00 .web_subtitle ,"web_key":O0O00OO00O0OOOO00 .web_key ,"web_description":O0O00OO00O0OOOO00 .web_description ,"web_domain":O0O00OO00O0OOOO00 .web_domain ,'web_logo':O0O00OO00O0OOOO00 .web_logo ,"web_ico":O0O00OO00O0OOOO00 .web_ico ,"web_stat":O0O00OO00O0OOOO00 .web_stat ,"web_close_tips":O0O00OO00O0OOOO00 .web_close_tips ,"web_company":O0O00OO00O0OOOO00 .web_company ,"web_contact_adders":O0O00OO00O0OOOO00 .web_contact_adders ,"web_contact_tel":O0O00OO00O0OOOO00 .web_contact_tel ,"web_info_copyright":O0O00OO00O0OOOO00 .web_info_copyright ,"web_record":O0O00OO00O0OOOO00 .web_record ,"web_contact_qq":O0O00OO00O0OOOO00 .web_contact_qq ,"web_contact_email":O0O00OO00O0OOOO00 .web_contact_email }}
    return jsonify (OO0O0000000O0O00O )
@webApi_bp .route ('/ad',endpoint ='ad',methods =['GET'])
@cache .cached (timeout =30 *(24 *(60 *60 )),key_prefix ='api_ad')
def ad_get ():
    O00OO0O00O0O00OO0 =request .args .get ('pid')
    O0OO0O000OOO0OOO0 =TL_Ad_Model .query .filter_by (uuid =O00OO0O00O0O00OO0 ,status =1 ).first ()
    if apiErrors (O0OO0O000OOO0OOO0 ):return apiErrors (O0OO0O000OOO0OOO0 )
    O00O0O0OO0000OOO0 =O0OO0O000OOO0OOO0 .ad_code if O0OO0O000OOO0OOO0 .status else ''
    O000O00O0000O000O ={"status":0 ,"msg":'successful',"data":{'adCode':O00O0O0OO0000OOO0 ,'ad_name':O0OO0O000OOO0OOO0 .ad_name ,'ad_end_date':O0OO0O000OOO0OOO0 .ad_end_date .strftime ('%Y-%m-%d'),}}
    return jsonify (O000O00O0000O000O )
@webApi_bp .route ('/ad_js_<string:OO0O0OO00OOO0OOO0>',endpoint ='ad_js',methods =['GET'])
@ipAuthority
@cache .cached (timeout =48 *(60 *60 ),key_prefix ='api_ad_js')
def ad_js_get (OO0O0OO00OOO0OOO0 ):
    O00O0OO0OOOO0O00O =TL_Ad_Model .query .filter_by (uuid =OO0O0OO00OOO0OOO0 ,status =1 ).first ()
    if check_obj (O00O0OO0OOOO0O00O ):return check_obj (O00O0OO0OOOO0O00O )
    OOO00000OO0O000O0 =f"""document.write("{O00O0OO0OOOO0O00O.ad_code}");"""
    O0OO0OOOOO0O00O00 =make_response (OOO00000OO0O000O0 )
    O0OO0OOOOO0O00O00 .headers ['Content-Type']='application/javascript'
    return O0OO0OOOOO0O00O00
@webApi_bp .route ('/recommend',endpoint ='recommend',methods =['GET'])
@ipAuthority
@authority
@cache .cached (key_prefix ='api_recommend')
def recommend ():
    O0O0O0000OOOO0000 =TL_Content_Model .query .order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status ==1 ,TL_Content_Model .is_recommend ==1 ,TL_Content_Model .update_time <datetime .now ()).all ()
    OOO0OO0O00000OO00 ={"status":0 ,'msg':'successful',"dataList":content (O0O0O0000OOOO0000 [:50 ],strftime ='%Y-%m-%d')}
    return jsonify (OOO0OO0O00000OO00 )
@webApi_bp .route ('/noticeDetail',endpoint ='noticeDetail',methods =['GET'])
@ipAuthority
@visiting_user
@cache .cached (timeout =30 *(24 *(60 *60 )),key_prefix ='api_noticeDetail')
def noticeDetail ():
    OO0O00O0OO0OO0OO0 =request .args .get ('pid')if request .args .get ('pid')else request .form .get ('pid')
    OO00O00OOO00O0O0O =TL_Notice_Model .query .filter_by (uuid =OO0O00O0OO0OO0OO0 ).first ()
    if apiErrors (OO00O00OOO00O0O0O ):return apiErrors (OO00O00OOO00O0O0O )
    OOO00OO0O0O00O0O0 ={"status":0 ,"msg":'successful',"data":{'title':OO00O00OOO00O0O0O .notice_title ,'promulgator':OO00O00OOO00O0O0O .notice_promulgator ,'content':OO00O00OOO00O0O0O .notice_content ,'date':OO00O00OOO00O0O0O .update_time .strftime ('%m-%d'),'id':OO00O00OOO00O0O0O .uuid ,}}
    return jsonify (OOO00OO0O0O00O0O0 )
@webApi_bp .route ('/comment',endpoint ='comment',methods =['POST'])
@ipAuthority
@authority
@conmmentAuthority
def comment ():
    OO0OOOO000O0O0O00 =request .json .get ('pid')if request .is_json else request .form .get ('pid')
    O0OOOOO000O0OOOOO =TL_Content_Model .query .filter_by (uuid =OO0OOOO000O0O0O00 ,status =1 ).first ()
    if apiErrors (O0OOOOO000O0OOOOO ):return apiErrors (O0OOOOO000O0OOOOO )
    O000OOO000OO0OOOO ={"status":0 ,"msg":'successful',"dataList":[{'comment_txt':O0O0OO0OOO0OO000O .comment_txt ,'user_qq':O0O0OO0OOO0OO000O .user_qq ,'user_name':O0O0OO0OOO0OO000O .user_name ,"create_time":O0O0OO0OOO0OO000O .update_time ,}for O0O0OO0OOO0OO000O in (O0OOOOO000O0OOOOO .comment_model [:100 ])[::-1 ]if O0O0OO0OOO0OO000O .status ]}
    return jsonify (O000OOO000OO0OOOO )
@webApi_bp .route ('/comment_post',endpoint ='comment_post',methods =['POST'])
@ipAuthority
@authority
@conmmentAuthority
def comment_post ():
    OO00000O0O0OO0000 =request .json .get ('content_uuid')if request .is_json else request .form .get ('content_uuid')
    O0OOOO0O0000OO0OO =TL_Content_Model .query .filter_by (uuid =OO00000O0O0OO0000 ,status =1 ).first ()
    if apiErrors (O0OOOO0O0000OO0OO ):return apiErrors (O0OOOO0O0000OO0OO )
    O0OO00OOOO0OOO00O =request .json .get ('comment_txt')if request .is_json else request .form .get ('comment_txt')
    if O0OO00OOOO0OOO00O and not len (O0OO00OOOO0OOO00O )>=5 :return jsonify ({"status":406 ,"msg":"内容长度不足!"})
    OOO00OOO0O0000OO0 =request .json .get ('user_qq')if request .is_json else request .form .get ('user_qq')
    O000OO0O000O0O000 =request .json .get ('user_name')if request .is_json else request .form .get ('user_name')
    OO0OOO0OOOOO0OOO0 =TL_Comment_Model ()
    try :
        OO0OOO0OOOOO0OOO0 .set_attrs ({"comment_txt":O0OO00OOOO0OOO00O ,"content_uuid":OO00000O0O0OO0000 ,"user_qq":OOO00OOO0O0000OO0 ,"user_name":O000OO0O000O0O000 })
        db .session .add (OO0OOO0OOOOO0OOO0 )
        db .session .commit ()
    except :
        db .session .rollback ()
        return jsonify ({"status":1 ,"msg":"评论发布失败!"})
    redis_ ().delete (OO00000O0O0OO0000 )
    return jsonify ({"status":0 ,"msg":"successful!","commentData":{"comment_txt":O0OO00OOOO0OOO00O ,"user_qq":OOO00OOO0O0000OO0 ,"user_name":O000OO0O000O0O000 ,"create_time":get_date ().strftime ('%Y-%m-%d'),}})
